#!/bin/sh
set -e -u

export LC_ALL=C

global_exclude='::RequireRcsKeywords'
tests_exclude='::(RequireCarping|ProhibitAutomaticExportation)'
private_exclude='::RequireCarping'
private_perl=$(grep -l '^#.*/perl' private/*)

here=$(basename "$0")
cd "$here/.."

sed -n -r -e '/^\[-?/ { s///; s/\]$//; p }' .perlcriticrc \
| sort -c

target=doc/todo.perlcritic

run_perlcritic()
{
    perlcritic --exclude "$global_exclude" --verbose '%p\n' "$@" >> "${target}.tmp" && return
    rc=$?
    if [ $rc -ne 2 ]
    then
        rm -f "${target}.tmp"
        exit $rc
    fi
}

: > "$target.tmp"
run_perlcritic "$@" mbank-cli
run_perlcritic "$@" \
    --exclude "$tests_exclude" \
    t/*.t t/*.pm
run_perlcritic "$@" \
    --exclude "$private_exclude" \
    $private_perl
sort "${target}.tmp" \
| grep -v ' source OK$' \
| uniq -c > "${target}.new"
rm -f "${target}.tmp"
diff -u "${target}" "${target}.new" || true
mv "${target}.new" "${target}"

# vim:ts=4 sts=4 sw=4 et
